var t = getApp()
Page({
  data: {
    list:[],
    good_id:null,
    navHeight:t.globalData.navHeight,
    title:'选择地址',
  },
  onLoad: function (options) {
    console.log(options);
    this.setData({
      good_id:options.good_id
    })
  },
  onShow(e){
    this.get_list()
  },
  onRefresh(){
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading(); 
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    this.get_list()
  },
  onPullDownRefresh: function () {
    //调用刷新时将执行的方法
    this.onRefresh();
    setTimeout(function () {
      // 不加这个方法真机下拉会一直处于刷新状态，无法复位
      wx.stopPullDownRefresh()
    }, 1000)
  },
  get_list(e){
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    t.request('/UserAddr/list',{},function(e){
      console.log(e);
      wx.hideLoading({
        success: (res) => {},
      })
      if (e.code==200) {
        if (e.result.list.length>0) {
          var list = e.result.list
          that.setData({
            list:list
          })
        }else if(e.result.list==''){
          that.setData({
            list:[]
          })
        }
      }
    })
  },
  to_add(e){
    var that = this
    wx.chooseAddress({
      success:function(res){
        var usemessage = res;
        console.log(usemessage);
        var o = {
          name:usemessage.userName,
          phone:usemessage.telNumber,
          location:usemessage.provinceName+usemessage.cityName+usemessage.countyName,
          address:usemessage.detailInfo,
        }
        t.request('/UserAddr/add',o,function(e){
          console.log(e);
          if (e.code==200) {
            that.get_list()
          }
        })
      },
      fail(e){
        console.log(e);
      }
    })

  },
  edit_addr(e){
    var item = JSON.stringify(this.data.list[e.currentTarget.dataset.index])
    wx.navigateTo({
      url: '/pages/personCenter/set/add_addr/add_addr?item=' + item,
    })
  },
  delete_addr(e){
    var that = this
    var id = that.data.list[e.currentTarget.dataset.index].id
    wx.showModal({
      title:'提示',
      content:'确认删除该地址吗？',
      success(e){
        if (e.confirm) {
          t.request('/UserAddr/delete',{id:id},function(e){
            console.log(e);
            if (e.code==200) {
              wx.showToast({
                title: '删除成功',
              })
              that.get_list()
            }
          })
        }
      }
    })

  },
  exchange(e){
    var that = this
    t.request('/goods/buy',{good_id:this.data.good_id,addr_id:e.currentTarget.dataset.id},function(e){
      console.log(e);
      if(e.code==200){
        wx.requestPayment({
          nonceStr: e.result.params.nonceStr,
          package: e.result.params.package,
          paySign: e.result.params.paySign,
          signType: 'MD5',
          timeStamp: e.result.params.timeStamp,
          success(e){
            wx.showToast({
              title: '兑换成功',
            })
            wx.navigateBack({
              delta: 1,
            })
          },
          fail(e){
            wx.showToast({
              title: '您取消了支付',
              icon:'none'
            })
          }
        })
        wx.showToast({
          title: '兑换成功',
        })
      }else{
        wx.showToast({
          title: e.msg,
          icon:'none'
        })
      }
    })
  }
})